<?php
/**                                          
 * 第三方登录(插件类的名称必须是{插件名_actions})
 * @author M2
 * @version 1.0
 * create Date 2013-05-02
 */
class LoginActions extends AbstractPlugins {
	public $version = '1.0'; // 版本号
	public $author = 'M2'; // 作者
	public $site = 'http://www.baidu.com';
	public $info = '这是一个支持多平台登录的插件'; // 描述信息
	public $pluginName = '多平台登录'; // 插件的中文名
	public $ylVersion = "1.0"; // 版本号
	public $isMenu = '1'; // 是否有后台管理面板
	private static $party = array (
			'qq' => '腾讯QQ',
			'tencent' => '腾讯微博',
			'sina' => '新浪微博',
			'renren' => '人人网',
			'douban' => '豆瓣网',
			'taobao' => '淘宝网',
			'google' => 'Google',
			'github' => 'Github',
			'msn' => 'Msn',
			'facebook' => 'Facebook',
	);
	private static $url = array (
			'qq' => 'http://open.t.qq.com/websites/',
			'tencent' => 'http://dev.t.qq.com/',
			'sina' => 'http://open.weibo.com/',
			'renren' => 'http://developer.renren.com',
			'douban' => 'http://www.douban.com/service/apidoc/connect',
			'taobao' => 'http://open.taobao.com', 
			'google' => 'https://developers.google.com',
			'github' => 'http://open.taobao.com',
			'msn' => 'http://msdn.microsoft.com/en-us/live',
			'facebook' => 'https://developers.facebook.com/',
	);
	
	private static $login = array(
			"qq"        => array("QQ_APP_KEY", "QQ_APP_SECRET"),
			"tencent"     => array("TENCENT_APP_KEY", "TENCENT_APP_SECRET"),
			"sina"      => array("SINA_APP_KEY", "SINA_APP_SECRET"),
			"renren"    => array("RENREN_APP_KEY", "RENREN_APP_SECRET"),
			"douban"    => array("DOUBAN_APP_KEY", "DOUBAN_APP_SECRET"),
			"taobao"    => array("TAOBAO_APP_KEY", "TAOBAO_APP_SECRET"),
			"google"    => array("GOOGLE_APP_KEY", "GOOGLE_APP_SECRET"),
			"github"    => array("GITHUB_APP_KEY", "GITHUB_APP_SECRET"),
			"msn"    => array("MSN_APP_KEY", "MSN_APP_SECRET"),
			"facebook"    => array("FACEBOOK_APP_KEY", "FACEBOOK_APP_SECRET"),
	);
	
	/**
	 * 解析函数的参数是pluginManager的引用
	 */
	public function __construct() {
		// 注册这个插件
		// 第一个参数是钩子的名称
		// 第二个参数是pluginManager的引用
		// 第三个是插件所执行的方法
		PluginManager::register ( 'login', $this, 'login' );
		PluginManager::register ( 'bind', $this, 'bind' );
	}
	
	/**
	 * 登录界面	
	 */
	public function login() {
		$this->assign('kk',$this->getBindArr());
		$this->display ( 'login', 'Login' );
	}
	
	/**
	 * 后台管理界面	
	 */
	public function menu() {
		$config = model('Data')->lget('login');
		$this->assign('config',$config);
		$this->assign('data',self::$login);
		$this->assign('party',self::$party);
		$this->assign('url',self::$url);
		$this->display ( 'config', 'Login' );
	}
	
	/**
	 * 插件安装	
	 */
	public function install() {
		$db_prefix = C ( 'DB_PREFIX' );
		$sql = "CREATE TABLE IF NOT EXISTS `{$db_prefix}login` (
		  `loginid` int(11) NOT NULL AUTO_INCREMENT,
		  `uid` int(11) DEFAULT NULL,
		  `openid` varchar(100) DEFAULT NULL,
		  `type` varchar(100) DEFAULT NULL,
		  `nick` varchar(100) DEFAULT NULL,
		  PRIMARY KEY (`loginid`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
		$res = M ()->execute ( $sql );
		if (false !== $res) {
			return true;
		}
	}
	
	/**
	 * 插件卸载	
	 */
	public function uninstall() {
		$db_prefix = C ( 'DB_PREFIX' );
		$sql = "DROP TABLE IF EXISTS `{$db_prefix}login`;DELETE FROM ".$db_prefix."system_data WHERE list='login';";
		
		$res = M ()->execute ( $sql );
		
		if (false !== $res) {
			return true;
		}
	}
	
	/**
	 * 获取已设置第三方登录数组
	 * @return multitype:unknown
	 */
	private function getBindArr(){
		$loginConfig = model('Data')->lget('login');
		$arr = array();
		foreach ($loginConfig as $k => $v) {
			$key = strtoupper($k);
			if(!empty($v[$key.'_APP_KEY']) && !empty($v[$key.'_APP_SECRET']) && $k !=="callback") {
				$arr[] = $k;
			}
		}
		return $arr;
	}
	/**
	 * 去掉数组中的重复值	
	 * @param array $arr
	 */
	function getNewArr($arr) {
		$tmpArr = array_count_values ( $arr );
		$newArr = array ();
		
		while ( list ( $key, $value ) = each ( $tmpArr ) ) {
			if ($value == 1) {
				array_push ( $newArr, $key );
			}
		}
		return $newArr;
	}
	
	/**
	 * 获取绑定和未绑定项	
	 */
	public function bind() {
		$uid = session ( 'mid' );
		$list = D ( 'Login' )->where ( "uid=$uid" )->order ( 'loginid desc' )->select ();
		$c = array (
				'腾讯QQ' => 'qq',
				'腾讯微博' => 'tencent',
				'新浪微博' => 'sina',
				'人人网' => 'renren',
				'豆瓣网' => 'douban',
				'淘宝网' => 'taobao', 
				'Google' => 'google',
				'Github' => 'github',
				'Msn' => 'msn',
				'Facebook' => 'facebook',
		);
		$a = array ();
		$b = array ();
		foreach ( $list as $k => $v ) {
			$a [] = array (
					'type' => array_search ( $v ['type'], $c ),
					'nick' => $v ['nick'],
					'openid' => $v ['openid'] 
			);
		}
		$bindarr = $this->getBindArr();
		foreach ( $c as $k1 => $v1 ) {
			if(in_array($v1,$bindarr)){
				$b [] = $k1;
			}
		}
		foreach ( $a as $k2 => $v2 ) {
			$b [] = $v2 ['type'];
		}
		$this->assign ( 'list', $a );
		$this->assign ( 'notbind', $this->getNewArr ( $b ) );
		$this->assign ('c',self::$party);
		$this->display ( 'bind', 'Login' );
	}
	
	/**
	 * 后台管理	
	 */
	public function c() {
		if ($_POST ['__hash__'])
			unset ( $_POST ['__hash__'] );
		$config = model('Data')->lput('login',$_POST);
		if($config) {
			$this->success('保存成功');
		} else {
			$this->error('保存失败');
		}
	}
}
?>